package org.xiaojinlong.reading;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.DefaultBHttpClientConnection;
import org.apache.http.impl.DefaultBHttpServerConnection;
import org.apache.http.message.BasicHttpResponse;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by jinlong on 2015/2/12.
 */
public class Server {
    public static void main(String[] args) throws IOException, HttpException {
        Socket socket = new Socket("", 8888);
        DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection(8 * 1024);
        conn.bind(socket);
        while (true) {
            HttpRequest request = conn.receiveRequestHeader();
            System.out.println(request);
            if (request != null) {
                HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
                response.setEntity(new StringEntity("Got it"));
                conn.sendResponseHeader(response);
                conn.sendResponseEntity(response);
            }
        }
    }
}
